﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Linq;
using System.Linq;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using UniMana.Data;
using UniMana.Utils.Data;
using UniMana.Utils.Controls;
using UniMana.Utils.Helper;

namespace UniMana.Win.Personnel
{
    public partial class FrmAttendance_SubAddEmployee : DevExpress.XtraEditors.XtraForm
    {
        DBMain m_data = new DBMain();
        int year, month, deptKey;

        private string m_Rule = UniMana.Utils.Configs.RuleConfig.ATT_EEP_ADD;

        public FrmAttendance_SubAddEmployee(string year, string month, int deptKey)
        {
            //if (!UniMana.Lib.Sys.Utils.CheckAccessByRule(m_Rule))
            //{
            //    this.Close();
            //    return;
            //}
            InitializeComponent();


            this.year = int.Parse(year);
            this.month = int.Parse(month);
            this.deptKey = deptKey;
            this.LoadData();
        }

        public void LoadData()
        {
            lueCatalogue_Department.GetData(m_data.m_DCReadOnly.Catalogue_Departments);
            lueCatalogue_Department.EditValue = this.deptKey;
            cmbMonth.SelectedIndex = month - 1;
            cmbYear.SelectedIndex = year - 2012;

            lueCatalogue_Department.Enabled = false;
            cmbYear.Enabled = false;
            cmbMonth.Enabled = false;

            DataTable tbl = new DataTable();
            tbl = (from p in
                           (
                              from pr in m_data.m_DCReadOnly.Personnel_Personnels
                              where pr.Personnel_PersonnelTypeKey != 2 
                                 && pr.Catalogue_DepartmentKey == lueCatalogue_Department.GetIntValue()
                                 && pr.Personnel_PersonnelTypeKey!=2
                                 && pr.Catalogue_PersonnelStatusKey ==1
                              select new
                              {
                                  pr.Personnel_PersonnelKey,
                                  pr.ID,
                                  pr.Name,
                                  pr.LastName,
                                  pr.Catalogue_DepartmentKey,
                                  IsCheck = false
                              })
                       where !(from prds in m_data.m_DCReadOnly.Personnel_Attendance_DetailV2s
                               join prdt in m_data.m_DCReadOnly.Personnel_AttendanceV2s
                               on prds.Personnel_AttendanceKey equals prdt.Personnel_AttendanceKey
                               where prdt.Catalogue_DepartmentKey == lueCatalogue_Department.GetIntValue()
                                     && prdt.Year == this.year && prdt.Month==this.month
                               select prds.Personnel_PersonnelKey).Contains(p.Personnel_PersonnelKey)
                       select p).ToDataTable();
            this.gctAttendanceAdd.DataSource = tbl;
        }

        private void xucBar1_btnAddNew_Click(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {

            ////if (!UniMana.Lib.Sys.Utils.CheckSaveByRule(this.m_Rule))
            ////{
            ////    xucBar1.dlg.Close();
            ////    return;
            ////}

            this.Validate();
            Personnel_AttendanceV2Info attInfo = new Personnel_AttendanceV2Info(year, month, lueCatalogue_Department.GetIntValue());
            if (attInfo.Properties.Personnel_AttendanceKey != null)
            {
                int attKey = attInfo.Properties.Personnel_AttendanceKey;
                DataTable tbl = (gctAttendanceAdd.DataSource as DataTable);
                if (tbl!=null)
                {
                    var shifts = m_data.m_DCReadOnly.Catalogue_Shifts;
                    foreach (DataRow r in tbl.Rows)
                    {
                        if (bool.Parse(r["IsCheck"].ToString()))
                        {
                            string persKey = r["Personnel_PersonnelKey"].ToString();
                            foreach (var shift in shifts)
                            {
                                Personnel_AttendanceDetailV2Info attDetailInfo = new Personnel_AttendanceDetailV2Info(attKey, persKey,shift.Catalogue_ShiftsKey);
                                attDetailInfo.Save();
                            }
                        }
                    }
                }
            }
        }

    }
}